package com.shipwe.loans.ui.presenter

import com.shipwe.loans.configs.ShipweConfigs
import com.shipwe.loans.http.ApiService
import com.shipwe.loans.http.ApiService.addCommonParams
import com.shipwe.loans.http.IHttpCallback
import com.shipwe.loans.pro.ShipwePresenter
import com.shipwe.loans.ui.view.IOrderRepayView
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/28 11:59
 * @Annotation：待还款 Presenter 实现
 */
class OrderRepayPresenter(private val iView: IOrderRepayView): ShipwePresenter(iView) {
    //详情
    var repayInfo: JSONObject? = null
    /**
     * 获取还款信息
     */
    fun loadOrderRepayInfoAction(){
        val pName = iView.getProductName()
        val pToken = iView.getProductToken()

        val httpUrl = ApiService.getHttpUrl()
        val params = JSONObject().addCommonParams().apply {
            put("path",ApiService.API_PATH.SHIPWE_API_LOAN_REPAYMENT_INFO_PATH)
            put("product",pName)
            put("token",pToken)
        }
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    repayInfo = obj
                    iView.showOrderRepayInfo(obj)
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }

    /**
     * 订单还款
     */
    fun loadOrderPayLinkAction(){
        val pName = iView.getProductName()
        val pToken = iView.getProductToken()

        val params = JSONObject().addCommonParams().apply {
            put("product",pName)
            put("token",pToken)
            put("mobile", ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_MOBILE))
            put("path",ApiService.API_PATH.SHIPWE_API_LOAN_REPAYMENT_URL_PATH)
        }
        if (ShipweConfigs.isUgx() || ShipweConfigs.isKsh()){//肯尼亚、乌干达 需要使用 getOrderRepayH5 获取链接
            params.put("path",ApiService.API_PATH.SHIPWE_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH)
        }
        //是否使用积分抵扣
        var usePoints = ""
        if (repayInfo?.has("isUsePoints") == true && repayInfo?.optBoolean("isUsePoints",false) == true){
            usePoints = repayInfo?.optString("usePoints","").toString()
            params.put("annulType","score")//不减免 normal  积分减免 score  优惠券减免 coupon
            params.put("score",usePoints)
        }
        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.dismissShipweLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null){
                    var linkUrl = obj.optString("url","")
                    if (linkUrl.isEmpty()){
                        linkUrl = obj.optString("repayUrl","")
                    }

                    //没有还款链接 - 需要进入还款状态页查询还款状态
                    if (linkUrl.isEmpty()){
                        val thirdOrderId = obj.optString("thirdOrderId")
                        iView.showOrderPaymentStatusPage(thirdOrderId,usePoints)
                    } else {
                        iView.showOrderPayLink(linkUrl)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }
}